from auxillaries.periodic_task import PeriodicTask
from auxillaries.unit_converter import UnitConverter
from abstract_node.datasheet_parser import SensorDataSheetParser
import time
import math
import random

class SensorInterface(PeriodicTask):

    def __init__(self, sensorDataSheet, callbackfct=[], logger=[]):
        self.parser=SensorDataSheetParser(filename=sensorDataSheet)
        self.properties=self.parser.load_datasheet()
        self.measurementFinishedClb=callbackfct
        period=UnitConverter.convertInto(value=self.properties.period,
                                         unit='seconds')
        PeriodicTask.__init__(self,
                              float(self.properties.period), 
                              self.measurement_notification)
        
    def getproperties(self):
        return self.properties
        
    def start_measurements(self):
        self.startTimer()
        
    def stop_measurements(self):
        self.stopTimer()
        
    def set_notification_fcn(self, callbackfct):
        self.measurementFinishedClb=callbackfct
    
    def measurement_notification(self):
        measurement=self.aggregateMeasurements()
        self.measurementFinishedClb(measurement, time.time())
    
    def aggregateMeasurements(self):
        return 1

class SimulatedSensor(SensorInterface):

    def __init__(self, sensorDataSheet, position, orientation, logger=[]):
        SensorInterface.__init__(self, 
                                 sensorDataSheet=sensorDataSheet,
                                 logger = logger)
        self.position=position
        self.orientation=orientation

    def get_position(self):
        return self.position
    
    def set_position(self, position):
        self.position=position

    def get_orientation(self):
        return self.orientation
    
    def set_orientation(self, orientation):
        self.orientation=orientation

    def aggregateMeasurements(self):
        result=random.random()
        return result
    
    
class NIDAQSensor(SensorInterface):

    def __init__(self):
        pass
    
    def aggregateMeasurements(self):
        return 1


class Microphone(SensorInterface):
    
    def __init__(self):
        pass
    
    def aggregateMeasurements(self):
        return 1
